/*
 * [Ronny Abraham]
 * [ronnya@gmail.com]
 * CS530:DUI, Assignment [#1]
 */

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/*
 * ActorEditorView
 */
public class ActorEditorView
{

	ActorEditorView(ActorEditorController controller, Actor inputActor, JPanel previewPanel, JFrame parentFrame)
	{
		itsActorEditorController = controller;
		this.itsInputActor = inputActor;
		itsPreviewPanel = previewPanel;
		itsParentFrame = parentFrame;
		init();
	}
	
	private void init()
	{
		// create 6 labels and 6 controls to edit the actor
		
		// name
		itsNameLabel = new JLabel("Name");
		itsNameTextField = new JTextField(itsInputActor.name);
		
		itsNameTextField.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				JTextField textField = (JTextField)e.getSource();
				String name = textField.getText();
				itsInputActor.name = name;
			}
		});
		itsNameTextField.addKeyListener(new NameKeyListener(itsInputActor));
		// expression
		itsExpressionLabel = new JLabel("Expression");
		itsExpressionComboBox = new JComboBox(Expression.values());
		itsExpressionComboBox.setSelectedItem(itsInputActor.expression);
		itsExpressionComboBox.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				JComboBox combo = (JComboBox)e.getSource();
				Expression selectedExp = (Expression)combo.getSelectedItem();
				itsInputActor.expression = selectedExp;
				itsActorEditorController.itsPreviewController.geometryChanged(itsWidthSlider.getValue(), itsHeightSlider.getValue(), (Expression)itsExpressionComboBox.getSelectedItem());
			}
		});
		// shirt
		itsShirtLabel = new JLabel("Shirt Color");
		itsShirtColorComboBox = new JComboBox(ClothesColor.values());
		itsShirtColorComboBox.setSelectedItem(itsInputActor.shirtColor);
		itsShirtColorComboBox.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{	
				JComboBox combo = (JComboBox)e.getSource();
				ClothesColor selectedColor = (ClothesColor)combo.getSelectedItem();
				itsInputActor.shirtColor = selectedColor;
				itsActorEditorController.itsPreviewController.setShirtColor(selectedColor);
			}
		});
		// pants
		itsPantsLabel = new JLabel("Pants Color");
		itsPantsColorComboBox = new JComboBox(ClothesColor.values());
		itsPantsColorComboBox.setSelectedItem(itsInputActor.pantsColor);
		itsPantsColorComboBox.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{	
				JComboBox combo = (JComboBox)e.getSource();
				ClothesColor selectedColor = (ClothesColor)combo.getSelectedItem();
				itsInputActor.pantsColor = selectedColor;
				itsActorEditorController.itsPreviewController.setPantsColor(selectedColor);
			}
		});
		// width
		itsWidthLabel = new JLabel("Width");
		itsWidthSlider = new JSlider(JSlider.HORIZONTAL, 50, 200, 100);
		itsWidthSlider.setMajorTickSpacing(50);
		itsWidthSlider.setMinorTickSpacing(25);
		itsWidthSlider.setPaintTicks(true);
		itsWidthSlider.setPaintLabels(true);
		itsWidthSlider.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
		itsWidthSlider.setValue(itsInputActor.width);
		itsWidthSlider.addChangeListener(new ChangeListener()
		{
			public void stateChanged(ChangeEvent e)
			{
				JSlider slider = (JSlider)e.getSource();
				int width = slider.getValue();
				itsInputActor.width = width;
				itsActorEditorController.itsPreviewController.geometryChanged(width, itsHeightSlider.getValue(), (Expression)itsExpressionComboBox.getSelectedItem());
			}
		});
		// height
		itsHeightLabel = new JLabel("Height");
		itsHeightSlider = new JSlider(JSlider.HORIZONTAL, 75, 300, 150);
		itsHeightSlider.setMajorTickSpacing(50);
		itsHeightSlider.setMinorTickSpacing(25);
		itsHeightSlider.setPaintTicks(true);
		itsHeightSlider.setPaintLabels(true);
		itsHeightSlider.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
		itsHeightSlider.setValue(itsInputActor.height);
		itsHeightSlider.addChangeListener(new ChangeListener()
		{
			public void stateChanged(ChangeEvent e)
			{
				JSlider slider = (JSlider)e.getSource();
				int height = slider.getValue();
				itsInputActor.height = height;
				itsActorEditorController.itsPreviewController.geometryChanged(itsWidthSlider.getValue(), height, (Expression)itsExpressionComboBox.getSelectedItem());
			}
		});
		
		// create a panel with a GroupLayout, and drop in the labels and controls
		itsActorEditorPanel = new JPanel();
		GroupLayout grp = new GroupLayout(itsActorEditorPanel);
		itsActorEditorPanel.setLayout(grp);
		grp.setAutoCreateGaps(true);
		grp.setAutoCreateContainerGaps(true);
		GroupLayout.SequentialGroup hGroup = grp.createSequentialGroup();
		// make the layout nice :)
		hGroup.addGroup(grp.createParallelGroup().
				addComponent(itsNameLabel).addComponent(itsExpressionLabel).addComponent(itsShirtLabel).
				addComponent(itsPantsLabel).addComponent(itsWidthLabel).addComponent(itsHeightLabel));
		hGroup.addGroup(grp.createParallelGroup().addComponent(itsNameTextField).addComponent(itsExpressionComboBox).addComponent(itsShirtColorComboBox).
				addComponent(itsPantsColorComboBox).addComponent(itsWidthSlider).addComponent(itsHeightSlider));
		grp.setHorizontalGroup(hGroup);
		GroupLayout.SequentialGroup vGroup = grp.createSequentialGroup();
		
		vGroup.addGroup(grp.createParallelGroup(Alignment.BASELINE).
	            addComponent(itsNameLabel).addComponent(itsNameTextField));
		vGroup.addGroup(grp.createParallelGroup(Alignment.BASELINE).
	            addComponent(itsExpressionLabel).addComponent(itsExpressionComboBox));
	   vGroup.addGroup(grp.createParallelGroup(Alignment.BASELINE).
	            addComponent(itsShirtLabel).addComponent(itsShirtColorComboBox));
	   
	   vGroup.addGroup(grp.createParallelGroup(Alignment.BASELINE).
	            addComponent(itsPantsLabel).addComponent(itsPantsColorComboBox));
	   vGroup.addGroup(grp.createParallelGroup(Alignment.BASELINE).
	            addComponent(itsWidthLabel).addComponent(itsWidthSlider));
	   vGroup.addGroup(grp.createParallelGroup(Alignment.BASELINE).
	            addComponent(itsHeightLabel).addComponent(itsHeightSlider));
	   grp.setVerticalGroup(vGroup);
	   // end make the layout nice
		
		// create a dialog for editing an actor
		itsDialog = new JDialog(itsParentFrame, "Actor", true);
		itsDialog.getContentPane().setLayout(new BorderLayout());
		itsDialog.getContentPane().add(itsActorEditorPanel, BorderLayout.WEST);
		itsDialog.getContentPane().add(itsPreviewPanel,  BorderLayout.CENTER);
		
		// create the Ack panel panel and OK\Cancel buttons
	    JPanel ackPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
	    JButton cancelBtn = new JButton("Cancel");
	    cancelBtn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				itsDialog.setVisible(false);
			}
		});
	    ackPanel.add(cancelBtn);
	    JButton okBtn = new JButton("OK");
	    okBtn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				itsActorEditorController.actorChanged(itsInputActor);
				itsDialog.setVisible(false);
			}
		});
	    ackPanel.add(okBtn);
	    itsDialog.getContentPane().add(ackPanel, BorderLayout.SOUTH);
	}
	
	public JDialog itsDialog;
	private JPanel itsPreviewPanel;
	private JPanel itsActorEditorPanel;
	private Actor itsInputActor;
	private ActorEditorController itsActorEditorController;
	private JFrame itsParentFrame;
	private JLabel itsNameLabel, itsExpressionLabel, itsShirtLabel, itsPantsLabel, itsWidthLabel, itsHeightLabel;
	private JTextField itsNameTextField;
	private JComboBox itsExpressionComboBox, itsShirtColorComboBox, itsPantsColorComboBox;
	private JSlider itsWidthSlider, itsHeightSlider;
}

/*
 * NameKeyListener
 * listens to key types and modifies the actor's name
 */
class NameKeyListener implements KeyListener
{
	NameKeyListener(Actor actor)
	{
		this.actor = actor;
	}

	@Override
	public void keyTyped(KeyEvent e)
	{
	}

	@Override
	public void keyPressed(KeyEvent e)
	{	
	}

	@Override
	public void keyReleased(KeyEvent e)
	{	
		JTextField nameTextField = (JTextField)e.getSource();
		actor.name = new String(nameTextField.getText());
	}
	
	private Actor actor;
}
